#include "textbox.h"
#include <resources.h>

volatile TextBox *focused_textbox = NULL;

void __onPress(void *arg);

TextBox *TextBox_Create(u32 x, u32 y, u32 width, u32 height)
{
    TextBox *t;
    
    t = (TextBox *)malloc(sizeof(TextBox));
    if(!t)
        return NULL;
    
    t->body = CreateGObj(x, y, width, height, pCOLOR_WHITE);
    if(!t->body) {
        free(t);
        return NULL;
    }
    
    t->body->event_trigger_type = EVENT_TRIG_SINGLE;
    t->body->private_data = (void *)t;
    t->body->onPress = __onPress;
    
    t->max_len = 0;
    t->editable = true;
    t->focused = false;
    t->onFocus = NULL;
    
    return t;
}

int TextBox_AddtoPage(PAGE *p, TextBox *t)
{
    if(!t || !p)
        return -1;
    
    if(t->body)
        PGAddGObj(p, t->body);
    
    return 0;
}

inline void TextBox_SetLength(TextBox *t, u8 max_len)
{
    if(t)
        t->max_len = max_len;
}

inline void TextBox_SetEditable(TextBox *t, BOOLEAN e)
{
    if(t)
        t->editable = e;
}

void TextBox_SetFocused(TextBox *t, BOOLEAN f)
{
    if(t)
        t->focused = f;
        
    if(f) {
        if(focused_textbox) {
            focused_textbox->focused = false;
            if(focused_textbox->onUnfocus)
                focused_textbox->onUnfocus((void *)focused_textbox);
        }
        
        focused_textbox = t;
        if(focused_textbox->onFocus)
            focused_textbox->onFocus((void *)focused_textbox);
    }
    else {
        if(focused_textbox == t) {
            if(focused_textbox->onUnfocus)
                focused_textbox->onUnfocus((void *)focused_textbox);
            focused_textbox = NULL;
        }
    }
}

void TextBox_SetBackgroundColor(TextBox *t, COLOR *c)
{
    if(!t || !c)
        return;
    t->body->color = c;
    if(t->body->text && t->body->text->txt_count > 0 && t->body->text->string) {
        t->body->setText(t->body, t->body->text->string, t->body->text->size, 
                         pCOLOR_BLACK, FONT_START_ADDR, FONT_FILE_SIZE, t->body->text_align, NULL);
    }
}

int TextBox_SetText(TextBox *t, const unicode_t *string, int size, u8 align)
{
    int len;
    unicode_t *s = string;
    
    if(t) {
        len = uni_strlen(string);
        if(t->max_len > 0 && len > t->max_len) {
            s = (unicode_t *)malloc((t->max_len + 1) * sizeof(unicode_t));
            if(!s)
                return -1;
            s = wcsncpy(s, string, t->max_len);
        }

        t->body->setText(t->body, s, size, pCOLOR_BLACK, FONT_START_ADDR, FONT_FILE_SIZE, align, NULL);
        if(s != string)
            free(s);
        return 0;
    }
    return -2;
}

inline unicode_t *TextBox_GetText(TextBox *t)
{
    if(t && t->body) {
        return t->body->text->string;
    }
    return NULL;
}

int TextBox_Update(TextBox *t)
{
    if(!t)
        return -1;
        
    t->body->draw(t->body);
    return 0;
}

void __onPress(void *arg)
{
    TextBox *t;
    GOBJ *gobj = (GOBJ *)arg;
    
    if(!gobj)
        return;
        
    t = (TextBox *)gobj->private_data;
    if(!t)
        return;   
    if(t != focused_textbox)
        TextBox_SetFocused(t, true);
}
